package com.zhaogang.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zhaogang.dao.OrderMapper;
import com.zhaogang.dao.SaleGoodsMapper;
import com.zhaogang.dao.UserAccountMapper;
import com.zhaogang.dao.UserDetailsMapper;
import com.zhaogang.dto.SaleGoodsDetailDTO;
import com.zhaogang.entity.Order;
import com.zhaogang.entity.UserAccount;
import com.zhaogang.entity.UserDetails;
import com.zhaogang.service.UserInfoService;

/**
* @author
* @data
* @version
* @parameter
* @return
*/
@Service
public class UserInfoServiceImpl implements UserInfoService {
	
	@Autowired
	private OrderMapper orderMapper;
	@Autowired 
	private UserAccountMapper userAccountMapper;
	@Autowired
	private UserDetailsMapper userDetailsMapper;
	@Autowired
	private SaleGoodsMapper saleGoodsMapper;

	public UserDetails selectUserDetailsInfo(UserAccount user) {
		// TODO Auto-generated method stub
		UserAccount userAccount=userAccountMapper.selectUserSelective(user);
		System.out.println("用户账户信息："+userAccount);
		UserDetails result=null;
		if(userAccount!=null){
			int accountId=userAccount.getId();
			result=userDetailsMapper.queryUserDetailByuserAccount(accountId);
			if( result!=null)
			{
				System.out.println("用户详细信息"+result);
				return result;
			}else{
				System.out.println("用户详细信息为空");
				return null;
				
			}
		}
		
		return result;
	}

	public void updateUserInfo(Map<String, Object> map) {
		// TODO Auto-generated method stub
		int i=0,j=0;
		UserAccount userAccount=(UserAccount) map.get("userAccount");
		if(userAccount!=null)
		{
			 i=userAccountMapper.updateUserByKey(userAccount);
		}
		UserDetails userDetails=(UserDetails) map.get("userDetails");
		if(userDetails!=null)
		{
			Integer userAccountId=userDetails.getUserAccountId();
			UserDetails result=userDetailsMapper.queryUserDetailByuserAccount(userAccountId);
			if(result!=null)
			{
				j=userDetailsMapper.updateUserDetailsInfo(userDetails);
			}else{
				j=userDetailsMapper.insertSelective(userDetails);
			}
		}
		System.out.println("用户账号修改："+i+",用户详情修改："+j);
		
	}

	public int deleteOrder(int orderId) {
		// TODO Auto-generated method stub
		int i=orderMapper.deleteByPrimaryKey(orderId);
		return i;
	}


	public Map<String, Object> selectOrderInfoByCondition(Map<String, Object> map) {
		// TODO Auto-generated method stub
		return null;
	}
	

	public List<Order> selectOrderInfo(UserAccount user) {
		// TODO Auto-generated method stub
		List<Order> list =new ArrayList<Order>();
		list=orderMapper.selectUserOrderList(user);
		System.out.println(list);
		return list;
	}

	public SaleGoodsDetailDTO getSaleGoodsInfo(int saleGoodsId) {
		// TODO Auto-generated method stub
		SaleGoodsDetailDTO saleGoodsDetail=new SaleGoodsDetailDTO();
		try {
			
			saleGoodsDetail=saleGoodsMapper.selectSaleGoodsInfo(saleGoodsId);
		} catch (NullPointerException e) {
			// TODO: handle exception
			System.out.println("商户商品详情为空");
		}
		
		return saleGoodsDetail;
	}

	public int updateAccountPW(List<String> list) {
		// TODO Auto-generated method stub
		int result=0;
		String newpsw=list.get(0);
		String oldpsw=list.get(1);
		String account=list.get(2);
		UserAccount userAccount =new UserAccount();
		userAccount.setUserAccount(account);
		userAccount=userAccountMapper.selectUserSelective(userAccount);
		if(userAccount!=null){
			if(oldpsw.equals(userAccount.getPassword()))
			{
				result=1;
				userAccount.setPassword(newpsw);;
				userAccountMapper.updateUserByKey(userAccount);
			}else {
				result=2;
			}
		}
			
		
		return result;
	}

}
